package com.szholly.pro.watergas_server.ps.android.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang3.ObjectUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.utils.spring.SpringBeanFactory;
import com.szholly.utils.util.JSONUtils;
import com.szholly.utils.util.StringUtils;
import com.szholly.data.common.util.ConvertCaptionUtils;
import com.szholly.data.common.util.JSONConvertUtils;
import com.szholly.data.general.GlobalParameter;
import com.szholly.data.general.ITable;
import com.szholly.data.general.ITableFactory;
import com.szholly.data.general.RowBase;
import com.szholly.data.general.interceptor.DbEditType;
import com.szholly.plug.form.core.interceptor.FormEditInterceptorClass;

import lrq.common.PropertiesUnit;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@Service
public class SewageService {
	private String token = "android_token";
	
	public JSONArray getSewageTaskList(String factoryId){
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		parms.add(new GlobalParameter("factoryId", factoryId));
		String sql = "select * from ("
				+ "select t.code,t.name,t.flow_type,t1.JOB_ID,t1.JOB_NAME,NVL(t2.NUM,0) NUM from BC_SITE_INFO t "
				+ "LEFT JOIN (select t.STATION_ID,t.JOB_ID,t1.JOB_NAME from OC_TASK_CONFIG t "
				+ "left join operation_job t1 on t.job_id = t1.guid) t1 on t1.STATION_ID = t.code  "
				+ "LEFT JOIN(select count(t.station_id) as NUM,t.job_id,t1.code from OP_TASK t  left join BC_SITE_INFO t1 on t.station_id = t1.exp_no "
				+ "where TASK_STATUS = '0'and t1.WWTP_ID = @factoryId group by t.job_id,t1.code)t2 ON t2.code = t.code AND t2.job_id =t1.JOB_ID "
				+ "WHERE t.WWTP_ID = @factoryId union all "
				+ "select t.code,t.name,t.flow_type,'123456' as JOB_ID,'异常处置' as JOB_NAME,NVL(t1.NUM,0) from BC_SITE_INFO t LEFT JOIN ( "
				+ "select count(t3.wwtp_id) as NUM,'123456' as job_id ,t3.code from OP_ALAR_INFO t2 "
				+ "left join BC_SITE_INFO t3 on t2.station_code = t3.exp_no where t3.wwtp_id = @factoryId and t2.STATUS = '0'and is_recover = '0'  "
				+ "group by t3.wwtp_id,t3.code)t1 on t1.code = t.code WHERE t.WWTP_ID = @factoryId)t WHERE 1 = 1 ORDER BY t.FLOW_TYPE，t.Job_name";
		List<RowBase> rows = null;
		try {
			rows = DataUtil.getSelectRows(sql, parms);
		} catch (Exception e) {
			
		}
		if(!CollectionUtils.isEmpty(rows)){
			return JSONArray.fromObject(rows);
		}else{
			return null;
		}
		
	}
	
	
	
	
	//--------------------------------------------------
		/**
		 * 获取污水厂站点数
		 * @param userID
		 * @param groupId
		 * @return
		 * @throws Exception
		 */
		public String getWsclcData(String userID, String groupId) throws Exception {
			List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
			String sql = "";
			if (groupId != null && !groupId.equals("")) {
				sql = "select sum(t.sum) as sum, t.name, t.exp_no\n"
						+ "  from (select sum(t.sum) as sum, t.name, t.exp_no\n"
						+ "          from (select t1.name, t1.exp_no,\n"
						+ "                (case t7.task_status when '0' then 1  else 0 end) as sum,t7.task_status\n"
						+ "                  from BC_ENTERPRISE_ATTR t1\n"
						+ "                  left join BC_SITE_INFO t2 on t1.exp_no = t2.wwtp_id\n"
						+ "                  left join OPERATION_GROUP_STATION_ASSO t3 on t2.exp_no =t3.station_id\n"
						+ "                  left join OC_GROUP t4 on t3.operation_group_id =t4.guid\n"
						+ "                  left join OPERATION_TASK t7 on t3.station_id =t7.station_id\n"
						+ "                 where 1 = 1 and t2.station_category='0' and t4.guid='" + groupId
						+ "') t where 1=1  group by t.name, t.exp_no union all"
						+ " select 1 as sum, t3.name as name, t2.wwtp_id as exp_no from BC_ENTERPRISE_ATTR t3"
						+ " inner join BC_SITE_INFO t2 on t2.wwtp_id = t3.exp_no"
						+ " inner join OP_ALAR_INFO t1 on t1.station_code = t2.exp_no"
						+ " left join OPERATION_GROUP_STATION_ASSO t4 on t3.exp_no =t4.station_id"
						+ " left join OC_GROUP t5 on t4.operation_group_id =t5.guid "
						+ " where t1.status = '0' and  t1.is_recover='1' and t2.station_category='0' and t5.guid='"
						+ groupId + "' group by t2.wwtp_id, t3.name ) t group by t.exp_no, t.name";
			} else {
				sql = "select sum(t.sum) as sum, t.name, t.exp_no\n"
						+ "  from (select sum(t.sum) as sum, t.name, t.exp_no\n" + "          from (select t1.name,\n"
						+ "                       t1.exp_no,\n" + "                       (case t7.task_status\n"
						+ "                         when '0' then\n" + "                          1\n"
						+ "                         else\n" + "                          0\n"
						+ "                       end) as sum,\n" + "                       t7.task_status\n"
						+ "                  from BC_ENTERPRISE_ATTR t1\n"
						+ "                  left join BC_SITE_INFO t2 on t1.exp_no = t2.wwtp_id\n"
						+ "                  left join OPERATION_GROUP_STATION_ASSO t3 on t2.exp_no =\n"
						+ "                                                               t3.station_id\n"
						+ "                  left join OC_GROUP t4 on t3.operation_group_id =\n"
						+ "                                                  t4.guid\n"
						+ "                  left join OPERATION_TASK t7 on t3.station_id =\n"
						+ "                                                 t7.station_id\n"
						+ "                 where 1 = 1 and t2.station_category='0') t\n" + "         where 1=1 \n"
						+ "            " + "         group by t.name, t.exp_no\n" + "        union all\n"
						+ "        select 1 as sum, t3.name as name, t2.wwtp_id as exp_no\n"
						+ "          from BC_ENTERPRISE_ATTR t3\n"
						+ "          inner join BC_SITE_INFO t2 on  t2.wwtp_id = t3.exp_no\n"
						+ "          inner join  OP_ALAR_INFO t1 on t1.station_code = t2.exp_no \n"
						+ "         where t1.status = '0' and t2.station_category='0'\n"
						+ "         group by t2.wwtp_id, t3.name) t\n" + " group by t.exp_no, t.name";
			}

			List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
			if (rows != null && rows.size() >= 0) {
				return JSONUtils.toJSONString(rows);
			} else {
				return "";
			}
		}
	/**
	 * 获取站点ID和进出水口情况
	 * @param wwtp_id
	 * @return
	 */
		public String getStationId(String wwtp_id){
			List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
			if (CommonAndroidService.checkNull(wwtp_id)) {
				return "";
			}
			String sql = "select t.EXP_NO as STATION_ID,t.FLOW_TYPE from BC_SITE_INFO t where wwtp_id ='" + wwtp_id
					+ "'";
			List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
			if (rows != null && rows.size() >= 0) {
				return JSONUtils.toJSONString(rows);
			} else {
				return "";
			}
	}
		/**
		 * 获取站点在线设备情况
		 * @return
		 */
		public String getEquipmentByStation(String flow_type, String wwtp_id){
			List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
			String sql = "select t1.name,t1.DEVICE_TYPENUM,t1.DEVICE_NUM from BC_SITE_DEVICE_RPT t1 "
					+ "left join BC_SITE_INFO t2 on t1.monitor_point=t2.exp_no where 1=1 ";
			if (flow_type != null && !flow_type.equals("")) {
				sql += " and t2.flow_type =@flow_type";
				parms.add(new GlobalParameter("flow_type", flow_type));
			}
			if (wwtp_id != null && !wwtp_id.equals("")) {
				sql += " and t2.wwtp_id =@wwtp_id";
				parms.add(new GlobalParameter("wwtp_id", wwtp_id));
			}
			List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
			if (rows != null && rows.size() >= 0) {
				return JSONUtils.toJSONString(rows);
			} else {
				return "";
			}
		}
		/**
		 * 获取签到次数
		 * 
		 * @param GROUP_ID
		 * @return
		 * @throws Exception
		 */
		public String getRegistrationCount(String GROUP_ID, String WTTP_ID) throws Exception {
			String sql = "select count(*) as sum from OP_REGI_INFO  where 1=1  ";
			List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
			if (GROUP_ID != null && !GROUP_ID.equals("")) {
				sql += " and GROUP_ID =@GROUP_ID";
				parms.add(new GlobalParameter("GROUP_ID", GROUP_ID));
			}
			if (WTTP_ID != null && !WTTP_ID.equals("")) {
				sql += " and WTTP_ID =@WTTP_ID ";
				parms.add(new GlobalParameter("WTTP_ID", WTTP_ID));
			}
			sql += " and to_date(to_char(time,'yyyy-mm'),'yyyy-mm') >to_date(to_char(add_months(trunc(sysdate),-1),'yyyy-mm'),'yyyy-mm')";
			List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
			JSONObject jo = new JSONObject();
			if (rows != null && rows.size() >= 0) {
				jo.put("SUM", rows.get(0).getValue("SUM").toString());
				return JSONUtils.toJSONString(jo);
			} else {
				return "";
			}
		}
		/**
		 * 获取污水厂范围坐标
		 * 
		 * @param exp_no
		 * @return
		 */
		public String getWorksCoords(String STATION_CODE) {
			List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
			String sql = "select * from STATION_INFO where 1=1 ";
			if (null != STATION_CODE) {
				sql += " and STATION_CODE = @STATION_CODE";
				parms.add(new GlobalParameter("STATION_CODE", STATION_CODE));
			}
			List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
			if (rows != null && rows.size() >= 0) {
				return JSONUtils.toJSONString(rows);
			} else {
				return "";
			}

		}
		/**
		 * 污水厂签到
		 * 
		 * @param userName
		 * @param password
		 * @return
		 * @throws Exception
		 */
		@RequestMapping("/getSignInResult")
		@ResponseBody
		public String getSignInResult(String token, String userID, String tableName, String row, String dataType,
				String TASK_CODE, String CREATE_TIME, String FINISH_BY, String DISTANCE) throws Exception {
			List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
			if (null == DISTANCE || DISTANCE.isEmpty()) {
				return "{\"result\":false}";
			}
			String signInRange = PropertiesUnit.getValue("distance");
			if (Float.parseFloat(DISTANCE) <= Integer.parseInt(signInRange)) {
				String result = null;
				try {
					RowBase entity = JSONConvertUtils.convertToEntity(row);
					Object obj = entity.getValue("_IS_DOMAIN_TRUE");
					if (obj != null && obj.toString().toLowerCase().equals("true")) {
						result = null;
					} else {
						result = tableSave(tableName, entity);
						if (!TASK_CODE.isEmpty() && !TASK_CODE.equals("")) {
							updateTaskStatus(TASK_CODE, "1", CREATE_TIME, FINISH_BY);
						}

					}
				} catch (Exception e) {
					e.printStackTrace();
					throw e;
				}
				return "{\"result\":true}";
			}
			return "{\"result\":false}";

		}
		/**
		 * 更新任务状态 wangl
		 * 
		 * @param TASK_CODE
		 * @param TASK_STATUS
		 * @param CREATE_TIME
		 * @return
		 */
		@RequestMapping("/updateTaskStatus")
		@ResponseBody
		public String updateTaskStatus(String TASK_CODE, String TASK_STATUS, String CREATE_TIME, String FINISH_BY) {
			String sql = "update  OPERATION_TASK set task_status='1',CREATE_TIME=to_date('" + CREATE_TIME
					+ "','yyyy-mm-dd hh24:mi:ss'),FINISH_TIME=to_date('" + CREATE_TIME + "','yyyy-mm-dd hh24:mi:ss')"
					+ ",FINISH_BY='" + FINISH_BY + "'" + " where task_code='" + TASK_CODE + "' ";
			List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
			int i = DataUtil.ExecuteUpdateSql(sql, parms);
			if (i == 0) {
				return "{\"result\":true}";
			} else {
				return "{\"result\":false}";
			}

		}
		/*
		 * 字典转译
		 */
		private String tableSave(String tableName, RowBase entity) throws Exception {
			ITableFactory tableFactory = (ITableFactory) SpringBeanFactory.getBean("TableFactory");
			ITable pTable = tableFactory.openTable(tableName);

			String returnString = FormEditInterceptorClass.BeforeDbEditOptionEx(pTable, entity, DbEditType.Insert);
			if (StringUtils.IsNullOrSpace(returnString)) {
				pTable.startEditing();
				try {
					pTable.save(entity);

					// 再做一次字典转议
					ConvertCaptionUtils.convertCaptionField(pTable, entity);
					ObjectMapper mapper = new ObjectMapper();
					String userMapJson = mapper.writeValueAsString(entity);
					pTable.stopEditing(true);

					return userMapJson;
				} catch (Exception e) {
					pTable.stopEditing(false);
					throw e;
				}
			} else {
				return returnString;
			}
		}
		/**
		 * 根据事件Id获取所有任务
		 * 
		 * @param jobId
		 * @return
		 */
		@RequestMapping("/getTaskStatusById")
		@ResponseBody
		public String getTaskStatusById(String jobId, String station_id, String flowType, String task_type) {
			checkToken(token);
			String result = "";
			if (task_type.equals("0")) {
				result = getTaskStatusData(jobId, station_id, flowType);
			}
			if (task_type.equals("1")) {
				result = getAbnormalDisposalData(jobId, station_id, flowType);
			}
			return result;
		}
		/**
		 * 获取待办污水厂运营任务
		 * 
		 * @param jobId
		 * @param station_id
		 * @param flowType
		 * @return
		 */
		private String getTaskStatusData(String jobId, String station_id, String flowType) {
			List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
			// 所有任务语句
			String ProcessedSql = "select t7.task_code,t7.task_status,t2.name as station_name,t7.job_id, t1.name,to_char(t7.start_time,'yyyy-mm-dd hh24:mi:ss') as start_time ,to_char(t7.end_time,'yyyy-mm-dd hh24:mi:ss') as end_time,t8.TASK_TOLERANCE ,"
					+ " to_char(t7.FINISH_TIME,'yyyy-mm-dd hh24:mi:ss') as FINISH_TIME,t1.exp_no\n"
					+ " from BC_ENTERPRISE_ATTR t1\n" + " left join BC_SITE_INFO t2 on t1.exp_no = t2.wwtp_id\n"
					+ " left join OPERATION_GROUP_STATION_ASSO t3 on t2.exp_no =\n" + "  t3.station_id";
			//只显示当月的数据
			ProcessedSql += " left join (select * from OP_TASK t where  t.create_time >= trunc(sysdate ,'MM') AND t.create_time <= last_day(sysdate)) t7 on t3.station_id = t7.station_id";
			//任务配置表
			ProcessedSql += " left join OC_TASK_CONFIG t8 on t7.station_id = t8.station_id where 1 = 1";
			
			if (jobId != null && !jobId.equals("")) {
				ProcessedSql += " and t7.JOB_ID =@jobId";
				ProcessedSql += " and t8.JOB_ID =@jobId";
				parms.add(new GlobalParameter("jobId", jobId));
			}
			if (station_id != null && !station_id.equals("")) {
				ProcessedSql += " and t7.station_id =@station_id";
				ProcessedSql += " and t8.station_id =@station_id";
				parms.add(new GlobalParameter("station_id", station_id));
			}
			List<RowBase> rowProcessed = DataUtil.getSelectRows(ProcessedSql, parms);
			JSONArray ProcessedData = new JSONArray();
			JSONArray PendingData = new JSONArray();
			JSONArray TimeoutHandlingData = new JSONArray();
			if (rowProcessed != null && rowProcessed.size() > 0) {
				for (int i = 0; i < rowProcessed.size(); i++) {
					HashMap<String, Object> d = rowProcessed.get(i);
					String task_status = d.get("TASK_STATUS").toString();
					Calendar deathCal = Calendar.getInstance();
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
					String et = d.get("END_TIME").toString();// 截止时间
					// String st=d.get("START_TIME").toString();//开始时间
					Date et_date = new Date();
					// Date st_date=new Date();
					Date fishtime_date = new Date();
					Date nowdate = new Date();
					try {
						et_date = sdf.parse(et);
						// st_date=sdf.parse(st);
						deathCal.setTime(et_date);
					} catch (ParseException e) {

						e.printStackTrace();
					}
					int timeCycleNum = Integer.parseInt(d.get("TASK_TOLERANCE").toString().replaceAll("[a-zA-Z]", ""));
					if (d.get("TASK_TOLERANCE").toString().indexOf("D") > 0) {
						deathCal.add(Calendar.DAY_OF_MONTH, timeCycleNum);
					} else if (d.get("TASK_TOLERANCE").toString().indexOf("M") > 0) {
						deathCal.add(Calendar.MONTH, timeCycleNum);
					}
					Date truefishtime = deathCal.getTime();

					if (task_status.equals("0")) {// 未处理
						// if (truefishtime.getTime() >= nowdate.getTime()) {
						PendingData.add(d);
						// } else {
						// TimeoutHandlingData.add(d);
						// }
					}
					if (task_status.equals("1")) {// 已处理
						String fishtime = d.get("FINISH_TIME").toString();// 完成时间
						try {
							fishtime_date = sdf.parse(fishtime);
						} catch (ParseException e) {

							e.printStackTrace();
						}
						ProcessedData.add(d);
						// 暂时屏蔽，不做超时判断
						/*
						 * if(fishtime_date.getTime()>=truefishtime.getTime()){
						 * ProcessedData.add(d); } else{ TimeoutHandlingData.add(d);
						 * }
						 */
					}
				}
			}

			JSONObject jsobj = new JSONObject();
			jsobj.put("PendingData", PendingData);
			jsobj.put("ProcessedData", ProcessedData);
			jsobj.put("TimeoutHandlingData", TimeoutHandlingData);
			List<JSONObject> allData = new ArrayList<>();
			allData.add(jsobj);
			String result = JSONUtils.toJSONString(allData);
			return JSONUtils.toJSONString(result);
		}
		/**
		 * 获取异常处置数据
		 * 
		 * @param jobId
		 * @param station_id
		 * @param flowType
		 * @param status
		 * @return
		 */
		private String getAbnormalDisposalData(String jobId, String station_id, String flowType) {
			checkToken(token);
			List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
			// String sql="select t1.* from OP_ALAR_INFO t1 left join
			// monitor_station t2 on t1.station_code = t2.exp_no where 1 = 1 ";

			String sql = "select * from (select t1.guid, t1.type,t1.alarm_value,t1.station_code,t1.status,'a' as fish_type,to_char((t1.createtime+1),'yyyy-mm-dd hh24:mi:ss')  "
					+ "as END_TIME,to_char((t1.createtime),'yyyy-mm-dd hh24:mi:ss')  as START_TIME,'123456' as job_id,d1.s_domaincaption as type_desc "
					+ "from OP_ALAR_INFO t1 " + "left join BC_SITE_INFO t2  on t1.station_code = t2.exp_no  "
					+ "left join DATA_DEFULATDOMAIN d1 on ( d1.S_DOMAINNAME = '报警类型' AND d1.S_DOMAINCODE = t1.type )\n"
					+ "where  t1.status = '0' and t1.is_recover='0' and t1.station_code='" + station_id
					+ "' order by t1.start_time desc)t where rownum < 11 \n" + "union all\n"
					+ "select * from (select t1.guid, t1.type,t1.alarm_value,t1.station_code,t1.status,'b' as fish_type,to_char((t1.createtime+1),'yyyy-mm-dd')  as END_TIME"
					+ ",to_char((t1.createtime),'yyyy-mm-dd')  as START_TIME,'123456' as job_id,d1.s_domaincaption as type_desc"
					+ " from OP_ALAR_INFO t1 " + "left join BC_SITE_INFO t2  on t1.station_code = t2.exp_no"
					+ " left join DATA_DEFULATDOMAIN d1 on ( d1.S_DOMAINNAME = '报警类型' AND d1.S_DOMAINCODE = t1.type )\n"
					+ "left join OP_ALAR_PROC t3 on t1.guid=t3.alarm_id\n"
					+ "where  t1.status = '1' and (t3.end_time-t1.createtime)  < 1 and t1.station_code='" + station_id
					+ "' order by t1.start_time desc)t where rownum < 11\n" + "union all\n"
					+ "select * from (select t1.guid, t1.type,t1.alarm_value,t1.station_code,t1.status,'c' as fish_type,to_char((t1.createtime+1),'yyyy-mm-dd')  as END_TIME,to_char((t1.createtime),'yyyy-mm-dd')  as START_TIME,'123456' as job_id,d1.s_domaincaption as type_desc from OP_ALAR_INFO t1 left join BC_SITE_INFO t2  on t1.station_code = t2.exp_no left join DATA_DEFULATDOMAIN d1 on ( d1.S_DOMAINNAME = '报警类型' AND d1.S_DOMAINCODE = t1.type )\n"
					+ "left join OP_ALAR_PROC t3 on t1.guid=t3.alarm_id\n"
					+ "where  t1.status = '1' and (t3.end_time-t1.createtime)  >1 and t1.station_code='" + station_id
					+ "' order by t1.start_time desc)t where rownum < 11";
			/*
			 * if(station_id != null && !station_id.equals("")){ sql+=
			 * " and t1.station_code =@station_code"; parms.add(new
			 * GlobalParameter("station_code",station_id)); } if(status != null &&
			 * !status.equals("")){ sql+=" and t1.STATUS =@status"; parms.add(new
			 * GlobalParameter("status",status)); }
			 */
			List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
			JSONArray ProcessedData = new JSONArray();// 正常时间已处理
			JSONArray PendingData = new JSONArray();// 待处理
			JSONArray TimeoutHandlingData = new JSONArray();// 超时已处理
			if (rows != null && rows.size() > 0) {
				for (int i = 0; i < rows.size(); i++) {
					HashMap<String, Object> d = rows.get(i);
					String FISH_TYPE = d.get("FISH_TYPE").toString();
					if (FISH_TYPE.equals("a")) {// 待处理
						PendingData.add(d);
					}
					if (FISH_TYPE.equals("b")) {// 已处理
						ProcessedData.add(d);
					}
					if (FISH_TYPE.equals("c")) {// 待处理
						TimeoutHandlingData.add(d);
					}
				}
				JSONObject jsobj = new JSONObject();
				jsobj.put("PendingData", PendingData);
				jsobj.put("ProcessedData", ProcessedData);
				jsobj.put("TimeoutHandlingData", TimeoutHandlingData);
				List<JSONObject> allData = new ArrayList<>();
				allData.add(jsobj);
				String result = JSONUtils.toJSONString(allData);
				return JSONUtils.toJSONString(result);
			} else {
				return "";
			}
		}
		/**
		 * 根据id获取污水异常项字典项
		 * 
		 * @param S_DOMAINNAME
		 * @return
		 */
		public String getDomainByID(String S_DOMAINNAME) {
			String sql = "select S_DOMAINCODE,S_DOMAINNAME,s_domaincaption,PY_PREFIX,s_id,s_pid ,TYPE from DATA_DEFULATDOMAIN where 1=1 ";// and
																																			// s_ischild='1'
			List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
			if (S_DOMAINNAME != null && !S_DOMAINNAME.equals("")) {
				sql += " and S_DOMAINNAME =@S_DOMAINNAME";
				parms.add(new GlobalParameter("S_DOMAINNAME", S_DOMAINNAME));
			}
			sql += " order by i_order";
			List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
			if (rows != null && rows.size() >= 0) {
				return JSONUtils.toJSONString(rows);
			} else {
				return "";
			}
		}
		/**
		 * 更新报警信息表
		 * 
		 * @param guid
		 * @param TASK_STATUS
		 * @return
		 */
		public String updateAlarmStatus(String guid, String RECEIVED_BY,String IS_RECOVER) {
			String sql = "update  OP_ALAR_INFO set STATUS='1', RECEIVED_BY='" + RECEIVED_BY + "' ,IS_RECOVER = '"+IS_RECOVER+"'  where GUID='"
					+ guid + "'";
			List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
			int i = DataUtil.ExecuteUpdateSql(sql, parms);
			if (i == 0) {
				return "{\"result\":true}";
			} else {
				return "{\"result\":false}";
			}

		}
		/**
		 * 获取污水日常巡检项
		 */
		public String getWaterOperation() throws Exception {
			List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
			String fatherSql = "select S_DOMAINCODE \"id\",S_DOMAINCAPTION \"name\" from data_defulatdomain where S_DOMAINNAME = '运维项' and I_JB ='2' and S_DOMAINCODE like 'A1%' ORDER BY S_DOMAINCODE";
			String childSql = "select S_DOMAINCODE \"id\",S_DOMAINCAPTION \"name\" from data_defulatdomain where S_DOMAINNAME = '运维项' and I_JB ='3'";
			// 1.先搜索污水2级目录
			List<RowBase> fatherList = DataUtil.getSelectRows(fatherSql, parms);

			// 响应结果
			List<HashMap<String, Object>> resultList = new ArrayList<HashMap<String, Object>>();

			if (fatherList != null && fatherList.size() > 0) {
				List<RowBase> childList;
				String sql;
				String id;
				String name;
				HashMap<String, Object> father;
				for (RowBase bean : fatherList) {
					try {
						id = bean.get("id").toString();
						name = bean.get("name").toString();
						sql = childSql + " and S_DOMAINCODE like '" + id + "%' ORDER BY S_DOMAINCODE";
						// 2.再搜索污水3级目录
						childList = DataUtil.getSelectRows(sql, parms);
						father = new HashMap<String, Object>();
						father.put("id", id);
						father.put("name", name);
						father.put("childList", childList);
						resultList.add(father);
					} catch (Exception e) {
					}
				}
				JSONArray jsonArray = JSONArray.fromObject(resultList);
				return jsonArray.toString();
			} else {
				return new JSONArray().toString();
			}
		}
		/**
		 * 根据污水站点获取在线设备
		 * 
		 * @param flow_type
		 * @param wwtp_id
		 * @return
		 */
		public String getSewageEquipment(String stationId) {
			List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
			String sql = "select t.name,t1.S_DOMAINCAPTION,t.DEVICE_TYPENUM,t.DEVICE_NUM,t.monitor_pollutants "
					+ "from BC_SITE_DEVICE_RPT t left join (select t.S_DOMAINCODE,t.S_DOMAINCAPTION  "
					+ "from data_defulatdomain  t  where t.S_DOMAINNAME='MONITOR_PARAM')t1 "
					+ "on t1.S_DOMAINCODE = t.monitor_pollutants where 1=1";
			if (stationId != null && !stationId.equals("")) {
				sql += " and t.monitor_point =@wwtp_id";
				parms.add(new GlobalParameter("wwtp_id", stationId));
			}
			List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
			if (rows != null && rows.size() >= 0) {
				return JSONUtils.toJSONString(rows);
			} else {
				return "";
			}
		}
		/**
		 * 根据站点进出水口获取在线设备
		 * 
		 * @param flow_type
		 * @param wwtp_id
		 * @return
		 */
		public String getSewageEquipmentByFlowtype(String flow_type, String wwtp_id) {
			List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
			String sql = "select t1.name,t1.DEVICE_TYPENUM,t1.DEVICE_NUM from BC_SITE_DEVICE_RPT t1 "
					+ "left join BC_SITE_INFO t2 on t1.monitor_point=t2.exp_no where 1=1 ";
			if (flow_type != null && !flow_type.equals("")) {
				sql += " and t2.flow_type =@flow_type";
				parms.add(new GlobalParameter("flow_type", flow_type));
			}
			if (wwtp_id != null && !wwtp_id.equals("")) {
				sql += " and t2.wwtp_id =@wwtp_id";
				parms.add(new GlobalParameter("wwtp_id", wwtp_id));
			}
			List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
			if (rows != null && rows.size() >= 0) {
				return JSONUtils.toJSONString(rows);
			} else {
				return "";
			}
		}
		/**
		 * 根据站点获取在线设备
		 * 
		 * @param flow_type
		 * @param wwtp_id
		 * @return
		 */
		@RequestMapping("/getEquipmentByStation1")
		@ResponseBody
		public String getEquipmentByStation1(String stationId) {
			List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
			String sql = "select t.name,t1.S_DOMAINCAPTION,t.DEVICE_TYPENUM,t.DEVICE_NUM,t.monitor_pollutants "
					+ "from BC_SITE_DEVICE_RPT t left join (select t.S_DOMAINCODE,t.S_DOMAINCAPTION  "
					+ "from data_defulatdomain  t  where t.S_DOMAINNAME='MONITOR_PARAM')t1 "
					+ "on t1.S_DOMAINCODE = t.monitor_pollutants where 1=1";
			if (stationId != null && !stationId.equals("")) {
				sql += " and t.monitor_point =@wwtp_id";
				parms.add(new GlobalParameter("wwtp_id", stationId));
			}
			List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
			if (rows != null && rows.size() >= 0) {
				return JSONUtils.toJSONString(rows);
			} else {
				return "";
			}
		}

		private void checkToken(String token) {
			if (!ObjectUtils.equals(token, this.token)) {
				throw new RuntimeException("认证错误！");
			}
		}
}
